package com.example.home.rubbish.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.rubbish.bean.RubbDetailBean;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.home.tool.StaticActi.ip;

public class RubbishNews extends AppCompatActivity {
    private ImageView imgUrl;
    private ImageView back;
    private TextView title;
    private TextView author;
    private TextView createTime;
    private TextView content;
    private EditText edImport;
    private Button btnSend;
    private String rnewsId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rubbish_news);
        Intent intent = getIntent();
        rnewsId = intent.getStringExtra("RnewsId");
        initView();
        getData();

    }

    private void getData() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news/" + rnewsId,
                OKHttpTool.GET,
                RubbDetailBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RubbDetailBean rubbDetailBean = (RubbDetailBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                RubbDetailBean.DataBean data = rubbDetailBean.getData();
                                Glide.with(RubbishNews.this).load(ip + data.getImgUrl()).into(imgUrl);
                                title.setText(data.getTitle());
                                author.setText(data.getAuthor());
                                createTime.setText(data.getCreateTime());
                                content.setText(HtmlCompat.fromHtml(data.getContent(), HtmlCompat.FROM_HTML_MODE_LEGACY));
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void initView() {


        imgUrl = findViewById(R.id.imgUrl);
        back = findViewById(R.id.back);
        title = findViewById(R.id.title);
        author = findViewById(R.id.author);
        createTime = findViewById(R.id.createTime);
        content = findViewById(R.id.content);
        edImport = findViewById(R.id.ed_import);
        btnSend = findViewById(R.id.btn_send);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RubbishNews.this.finish();
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String comment = edImport.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("newsId", rnewsId);
                    jsonObject.put("content", comment);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (!TextUtils.isEmpty(comment)) {
                    OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news-comment",
                            OKHttpTool.POST,
                            GeneralClass.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    GeneralClass generalClass = (GeneralClass) obj;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (!isFinishing()) {
                                                if (generalClass.getCode() == 200) {
                                                    Toast.makeText(RubbishNews.this, "评论成功", Toast.LENGTH_SHORT).show();
                                                } else if (generalClass.getCode() == 500) {
                                                    Toast.makeText(RubbishNews.this, "评论失败", Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {
                                }
                            }, jsonObject.toString());
                }
            }
        });

    }
}